package com.deepexps.security.utils;


import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.util.encoders.Hex;


/**
 * @program: iot-platform
 * @description: 密码加盐加密
 * @author: lvxiwei
 * @create: 2021-11-29 13:52
 **/
public class PasswordUtil {
    private static String SALT = "deepexps";

    private static String encode(Digest digest, String src, boolean isSalt) {
        if (isSalt) {
            src += SALT;
        }
        digest.update(src.getBytes(), 0, src.getBytes().length);
        byte[] md5Bytes = new byte[digest.getDigestSize()];
        digest.doFinal(md5Bytes, 0);
        return Hex.toHexString(md5Bytes);

    }

    public static String encodeMD5(String src) {
        return encode(new MD5Digest(), src, true);
    }

    public static String encodeMD5(String src, boolean isSalt) {
        return encode(new MD5Digest(), src, isSalt);
    }

    public static String encodeSHA1(String src) {
        return encode(new SHA1Digest(), src, true);
    }

    public static String encodeSHA256(String src) {
        return encode(new SHA256Digest(), src, true);
    }

    public static void main(String[] args) {
        System.out.println(encodeMD5("123456"));
    }
}
